<template>
  <el-select
    v-model="selectValue"
    placeholder="请选择"
    style="width:150px"
    clearable
    filterable
    @change="changeEvent"
  >
    <el-option
      v-for="(item,index) in productList"
      :key="index"
      :label="item.strproductname"
      :value="item.lngproductid"
    />
  </el-select>
</template>
<script>
import { getProducts } from '@/api/common/common'
export default {
  name: 'BillTemplateTypeSelect',
  props: {
    value: {
      type: [Number, String],
      default: ''
    },
    filterId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      productList: []
    }
  },
  computed: {
    selectValue: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  created() {
    this.getProducts()
  },
  methods: {
    changeEvent(val) {
      this.$emit('change', val)
      this.$emit('select', this.productList.find(el => { return el.lngproductid * 1 === val * 1 }))
    },
    getProducts() {
      getProducts().then(res => {
        if (res.code === 20000) {
          if (this.filterId) {
            this.productList = res.data.filter(item => {
              return item.lngproductid * 1 !== this.filterId * 1
            })
          } else {
            this.productList = res.data
          }
        }
      })
    }
  }
}
</script>
